package presentation.windows;
import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import java.awt.Dimension;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;

import util.Log;
import util.NotFoundException;
import util.Subsystem;
import domain.user.UnpermittedActionException;




/**
* add topic frame
*/
public class AddTopicFrame extends javax.swing.JFrame {

	private static final long serialVersionUID = 7459063499789582295L ;
	private JLabel topicLable;
	private JTextField topicTextField;
	private JButton addButton;
	private JButton cancelButton;
	private JLabel msgLable;
	private JTextArea msgTextArea;
	private ViewDirectoryPanel directoryPanel;
	private JScrollPane jScrollPane1;

	/**
	* adding a new topic (thread) screen
	 * @param directoryPanel - the directory to which we're adding the thread
	*/
	public AddTopicFrame(final ViewDirectoryPanel directoryPanel) {
		this.setPreferredSize(new Dimension(735, 566));
		this.initGUI();
		this.directoryPanel = directoryPanel;
	}

	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setTitle("Add New Topic");

			this.topicLable = new JLabel();
			this.topicLable.setText("Topic:");
			this.topicLable.setFont(new java.awt.Font("Californian FB",1,16));

			this.topicTextField = new JTextField();

			this.msgTextArea = new JTextArea();

			this.msgLable = new JLabel();
			this.msgLable.setText("Write your message here:");

			this.addButton = new JButton();
			this.addButton.setText("Add");
			this.addButton.setFont(new java.awt.Font("Californian FB",1,16));
			this.addButton.setPreferredSize(new java.awt.Dimension(117, 32));
			getContentPane().add(this.addButton, new AnchorConstraint(869, 402, 942, 236, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));

			this.addButton.addActionListener(new java.awt.event.ActionListener() {
		         @SuppressWarnings("synthetic-access")
				public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt) {
		        	 AddTopicFrame.this.addButtonHandler();
		         }
			 });
			
			this.cancelButton = new JButton();
			this.cancelButton.setText("Cancel");
			this.cancelButton.setFont(new java.awt.Font("Californian FB",1,16));
			this.cancelButton.setPreferredSize(new java.awt.Dimension(113, 32));
			getContentPane().add(this.cancelButton, new AnchorConstraint(869, 753, 942, 593, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			
			this.cancelButton.addActionListener(new java.awt.event.ActionListener() {
		         @SuppressWarnings("synthetic-access")
				public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt) {
		        	 AddTopicFrame.this.cancelButtonHandler();
		         }
			 });
			
			getContentPane().add(this.topicLable, new AnchorConstraint(75, 188, 121, 112, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.topicLable.setPreferredSize(new java.awt.Dimension(53, 20));
			getContentPane().add(this.topicTextField, new AnchorConstraint(75, 915, 123, 188, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.topicTextField.setPreferredSize(new java.awt.Dimension(512, 21));
			getContentPane().add(this.msgLable, new AnchorConstraint(171, 341, 202, 49, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.msgLable.setPreferredSize(new java.awt.Dimension(206, 14));
			
			this.jScrollPane1 = new JScrollPane();
			getContentPane().add(this.jScrollPane1, new AnchorConstraint(216, 972, 835, 39, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.jScrollPane1.setPreferredSize(new java.awt.Dimension(657, 273));
			this.jScrollPane1.setViewportView(this.msgTextArea);
			
			this.pack();
			this.setSize(712, 475);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void addButtonHandler(){
		if(this.topicTextField.getText()==null || this.topicTextField.getText().equals(""))
		{
			//no topic insertion:
			JOptionPane.showMessageDialog(AddTopicFrame.this,
				"please write the topic to add", "error occured", JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(this.msgTextArea.getText()==null || this.msgTextArea.getText().equals(""))
		{
			//no message insertion:
			JOptionPane.showMessageDialog(AddTopicFrame.this,
				"please write the first message of this new topic", "error occured", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		new SwingWorker<Void,Void>(){
			
			@SuppressWarnings("synthetic-access")
			@Override
			protected Void doInBackground() {

				//add this thread to directoryPanel:
				try
				{
					AddTopicFrame.this.directoryPanel.addThread(AddTopicFrame.this.topicTextField.getText(), AddTopicFrame.this.msgTextArea.getText());
					AddTopicFrame.this.dispose(); //close the window if operation succeeded
				}
				catch (RemoteException e)
				{
					Log.getLogger(Subsystem.PRESENTATION_MODEL).info("communication problem when trying to add thread.") ;
					//show error message:
					JOptionPane.showMessageDialog(AddTopicFrame.this,
						"problem with adding the thread", "error occured", JOptionPane.ERROR_MESSAGE);
				}
				catch (UnpermittedActionException e)
				{
					long userId = AddTopicFrame.this.directoryPanel.getSharedData().getUser().getId();
					Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+userId+" is unpermitted to add thread.") ;
					//show error message:
					JOptionPane.showMessageDialog(AddTopicFrame.this,
						"you are unpermitted to add new topics", "error occured", JOptionPane.ERROR_MESSAGE);
				}
				catch (NotFoundException e)
				{
					Log.getLogger(Subsystem.PRESENTATION_MODEL).info("the wanted directory to add thread in it could not found.") ;
					//show error message:
					JOptionPane.showMessageDialog(AddTopicFrame.this,
						"problem with adding the thread", "error occured", JOptionPane.ERROR_MESSAGE);
				}
				return null;
			}
			@Override
			protected void done(){/*nothing to do here */}
		}.execute();
	}

	private void cancelButtonHandler()
	{
		this.dispose(); //close the window
	}
	
	/**
	 * @see java.awt.Window#dispose()
	 */
	@Override
	public void dispose()
	{
		super.dispose() ;
		this.directoryPanel.removeOpenSubFrame(this);
	}

}
